<template>
  <m-table
  :data="tableData"
  :options="options"
  elementLoadingText="加载中"
  elementLoadingBackground="rgba(0,0,0,.8)"
  :element-loading-svg="svg"
  element-loading-svg-view-box="-10, -10, 50, 50"
  @confirm="confirmHandle"
  @cancel="cancelHandle"
  isEditRow
  pagination
  :total="total"
  v-model:editRowIndex="editRowIndex"
  justifyContent="center"
  @size-change="handleSizeChange"
  @current-change="handleCurrentChange"
  :highlightCurrentRow="highlightCurrentRow"
  @handle-select="handleSelect"
  @handle-selection="handleSelection"
  :defaultSort="{prop: 'date', order: 'descending'}"
  >
    <template #date="{scope}">
      <el-icon-timer></el-icon-timer>
      {{scope.row.date}}
    </template>
    <template #name="{scope}">
      <el-popover effect="light" trigger="hover" placement="top" width="auto">
          <template #default>
            <div>name: {{ scope.row.name }}</div>
            <div>address: {{ scope.row.address }}</div>
          </template>
          <template #reference>
            <el-tag>{{ scope.row.name }}</el-tag>
          </template>
        </el-popover>
    </template>
    <template #address="{scope}">
      {{scope.row.address}}
    </template>
    <template #action="{scope}">
      <el-button size="small" type="primary" @click="edit(scope)">编辑</el-button>
      <el-button size="small" type="danger" @click="del(scope)">删除</el-button>
    </template>
    <template #editRow="{scope}">
      <el-button size="small" type="primary">确认</el-button>
      <el-button size="small" type="danger">取消</el-button>
    </template>
<!--    <template #editCell="{scope}">-->
<!--      <div style="display: flex">-->
<!--        <el-button size="small" type="primary">确认</el-button>-->
<!--        <el-button size="small" type="info">取消</el-button>-->
<!--      </div>-->
<!--    </template>-->
  </m-table>
</template>

<script setup lang="ts">
import {TableOptions} from "../../components/table/src/type.ts";
import {onMounted, ref} from "vue";
import axios from 'axios';
let editRowIndex = ref<string>('')
let tableData = ref<any[]>([])
let highlightCurrentRow = ref<boolean>(true)
let current = ref<number>(1)
let pageSize = ref<number>(10)
let total = ref<number>(0)
let getData = () => {
  axios.post('/api/list', {
    current: current.value,
    pageSize: pageSize.value,
  }).then((res: any) => {
    if (res.data.code === '200') {
      tableData.value = res.data.data.rows
      total.value = res.data.data.total
      console.log(res.data.data)
    }
  })
}
let handleSizeChange = (val: number) => {
  pageSize.value = val
  getData()
}
let handleCurrentChange = (val: number) => {
  current.value = val
  console.log(val)
  getData()
}

onMounted(() => {
  getData()
})
// setTimeout(() => {
// tableData.value = [
//   {
//     date: '2016-05-03',
//     name: 'Tom1',
//     address: 'No. 189, Grove St, Los Angeles',
//   },
//   {
//     date: '2016-05-02',
//     name: 'Tom2',
//     address: 'No. 189, Grove St, Los Angeles',
//   },
//   {
//     date: '2016-05-04',
//     name: 'Tom3',
//     address: 'No. 189, Grove St, Los Angeles',
//   },
//   {
//     date: '2016-05-01',
//     name: 'Tom4',
//     address: 'No. 189, Grove St, Los Angeles',
//   },
// ]
// }, 3000)

let svg = `
        <path class="path" d="
          M 30 15
          L 28 17
          M 25.61 25.61
          A 15 15, 0, 0, 1, 15 30
          A 15 15, 0, 1, 1, 27.99 7.5
          L 15 15
        " style="stroke-width: 4px; fill: rgba(0, 0, 0, 0)"/>
      `

let options: TableOptions[] = [
  {
    prop: 'date',
    label: '日期',
    width: '300',
    align: 'center',
    slot: 'date',
    editable: true,
    sortable: true,
  },
  {
    prop: 'name',
    label: '姓名',
    width: '100',
    align: 'center',
    slot: 'name'
  },
  {
    prop: 'address',
    label: '地址',
    width:'200',
    align: 'center',
    editable: true
  },
  {
    label: '操作',
    action: true,
    align: 'center'
  }
]

let edit = (scope:any)=>{
  editRowIndex.value = 'edit'
}

let del = (scope:any)=>{
}

let confirmHandle=(scope:any)=>{
  console.log(scope)
}

let cancelHandle=(scope:any)=>{
  console.log(scope)
}

let handleSelect=(val:any)=>{
  console.log(val)
}

let handleSelection=(val:any)=>{
  console.log(val)
}
</script>

<style lang="scss" scoped>
svg{
  height: 1em;
  width: 1em;
}
</style>
